package com.graduation.cdm.funactivity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;

import com.graduation.cdm.funactivity.view.fragment.ActivityFragment;
import com.graduation.cdm.funactivity.view.fragment.HomeFragment;
import com.graduation.cdm.funactivity.view.fragment.MineFragment;

public class MainActivity extends AppCompatActivity {

    private long exitTime = 0;

    Fragment[] fragments;
    HomeFragment homeFragment;//主页
    ActivityFragment activityFragment;//活动
    MineFragment mineFragment ;//个人中心
    //MessageFragment messageFragment;//消息

    //按钮的数组，一开始第一个按钮被选中
    RadioButton[] tabs;
    int oldIndex;//用户看到的item
    int newIndex;//用户即将看到的item

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化fragment
        homeFragment=new HomeFragment();
        activityFragment=new ActivityFragment();
        //messageFragment= new MessageFragment();
        mineFragment=new MineFragment();
        //所有fragment的数组
        //fragments=new Fragment[]{homeFragment,activityFragment,messageFragment,mineFragment};
        fragments=new Fragment[]{homeFragment,activityFragment,mineFragment};


        //设置按钮的数组
        tabs=new RadioButton[3];
        tabs[0]=(RadioButton) findViewById(R.id.rb_rb1);//主页的button
        tabs[1]=(RadioButton) findViewById(R.id.rb_rb2);//活动的button
        tabs[2]=(RadioButton) findViewById(R.id.rb_rb4);//消息的button
        //tabs[3]=(RadioButton) findViewById(R.id.rb_rb4);//我的的button
        //界面初始显示第一个fragment;添加第一个fragment
        getSupportFragmentManager().beginTransaction().add(R.id.f1_content, fragments[0]).commit();
        //初始时，按钮1选中
        tabs[0].setChecked(true);

    }

    //按钮的点击事件:选中不同的按钮，不同的fragment显示
    public void onTabClicked(View view) {

        //点击按钮时，表示选中不同的项
        switch(view.getId()){
            case R.id.rb_rb1:
                newIndex=0;//选中第一项
                break;
            case R.id.rb_rb2:
                newIndex=1;//选中第二项
                break;
            case R.id.rb_rb4:
                newIndex=2;//选中第三项
                break;
        }
        addshow(newIndex);
    }

    public void addshow(int newIndex){
        FragmentTransaction transaction;

        //如果选择的项不是当前选中项，则替换；否则，不做操作
        if(newIndex!=oldIndex){
            transaction=getSupportFragmentManager().beginTransaction();
            transaction.hide(fragments[oldIndex]);//隐藏当前显示项


            //如果选中项没有加过，则添加
            if(!fragments[newIndex].isAdded()){
                //添加fragment
                transaction.add(R.id.f1_content,fragments[newIndex]);
            }
            //显示当前选择项
            transaction.show(fragments[newIndex]).commit();
        }


        //之前选中的项，取消选中
        tabs[oldIndex].setSelected(false);
        //当前选择项，按钮被选中
        tabs[newIndex].setSelected(true);

        //当前选择项变为选中项
        oldIndex=newIndex;
    }


}
